package com.ruoyi.oa.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.aftserv.domain.ManageGradeClass;
import com.ruoyi.aftserv.service.IManageGradeClassService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.oa.domain.ManageStudent;
import com.ruoyi.oa.service.IManageStudentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生Controller
 * 
 * @author 张艳羽
 * @date 2024-08-20
 */
@Controller
@RequestMapping("/system/student")
public class ManageStudentController extends BaseController
{
    private String prefix = "system/student";

    @Autowired
    private IManageStudentService manageStudentService;
    @Autowired
    private IManageGradeClassService manageGradeClassService;

    @RequiresPermissions("oa:student:view")
    @GetMapping()
    public String student(ModelMap mmap)
    {
        mmap.put("classes", this.manageGradeClassService.selectManageGradeClassList(new ManageGradeClass()));
        return prefix + "/student";
    }

    /**
     * 查询学生列表
     */
    @RequiresPermissions("oa:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ManageStudent manageStudent)
    {
        startPage();
        List<ManageStudent> list = manageStudentService.selectManageStudentList(manageStudent);
        return getDataTable(list);
    }

    /**
     * 导出学生列表
     */
    @RequiresPermissions("oa:student:export")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ManageStudent manageStudent)
    {
        List<ManageStudent> list = manageStudentService.selectManageStudentList(manageStudent);
        ExcelUtil<ManageStudent> util = new ExcelUtil<ManageStudent>(ManageStudent.class);
        return util.exportExcel(list, "学生数据");
    }

    /**
     * 新增学生
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("classes", this.manageGradeClassService.selectManageGradeClassList(new ManageGradeClass()));
        return prefix + "/add";
    }

    /**
     * 新增保存学生
     */
    @RequiresPermissions("oa:student:add")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ManageStudent manageStudent)
    {
        return toAjax(manageStudentService.insertManageStudent(manageStudent));
    }

    /**
     * 修改学生
     */
    @RequiresPermissions("oa:student:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ManageStudent manageStudent = manageStudentService.selectManageStudentById(id);
        mmap.put("manageStudent", manageStudent);
        mmap.put("classes", this.manageGradeClassService.selectManageGradeClassList(new ManageGradeClass()));
        return prefix + "/edit";
    }

    /**
     * 修改保存学生
     */
    @RequiresPermissions("oa:student:edit")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ManageStudent manageStudent)
    {
        return toAjax(manageStudentService.updateManageStudent(manageStudent));
    }

    /**
     * 删除学生
     */
    @RequiresPermissions("oa:student:remove")
    @Log(title = "学生", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(manageStudentService.deleteManageStudentByIds(ids));
    }

    /**
     * 校验学号
     */
    @PostMapping("/checkStunoUnique")
    @ResponseBody
    public boolean checkStunoUnique(ManageStudent student)
    {
        return this.manageStudentService.checkStunoUnique(student);
    }
}
